package org.anderground.editor.gui;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Point;
import java.awt.Rectangle;
import java.awt.event.MouseEvent;
import java.awt.event.MouseMotionAdapter;

import javax.swing.JPanel;

public class DividedLayout implements LayoutManager {
	private Component first, second;
	private float division;
	private int axis;
	private int space = 5;
	public static final int X_AXIS = 0, Y_AXIS = 1;

	public DividedLayout(Component first, Component second, JPanel parent,
			float dividion, int axis) {
		this.first = first;
		this.second = second;
		parent.add(first);
		parent.add(second);
		this.division = dividion;
		if (axis != X_AXIS && axis != Y_AXIS) {
			throw new RuntimeException("Wrong axis");
		}
		this.axis = axis;
	}

	@Override
	public void addLayoutComponent(String arg0, Component arg1) {
		if (first == null) {
			first = arg1;
		} else if (second == null) {
			second = arg1;
		} else {
			throw new RuntimeException("Only two component allowed");
		}
	}

	@Override
	public void layoutContainer(Container parent) {
		assert (axis == X_AXIS || axis == Y_AXIS);
		if (axis == X_AXIS)
			layoutXAxis(parent);
		else if (axis == Y_AXIS)
			layoutYAxis(parent);

	}

	private void layoutYAxis(Container parent) {
		int firstHeight = (int) (parent.getHeight() * division);
		int secondHeight = (int) (parent.getHeight() * (1 - division));
		first.setBounds(new Rectangle(0, 0, parent.getWidth(), firstHeight));
		second.setBounds(new Rectangle(0, space + firstHeight, parent
				.getWidth(), secondHeight - space));

	}

	private void layoutXAxis(Container parent) {
		int firstWidth = (int) (parent.getWidth() * division);
		int secondWidth = (int) (parent.getWidth() * (1 - division));
		first.setBounds(new Rectangle(0, 0, firstWidth, parent.getHeight()));
		second.setBounds(new Rectangle(space + firstWidth, 0, secondWidth
				- space, parent.getHeight()));
	}

	@Override
	public Dimension minimumLayoutSize(Container arg0) {

		return null;
	}

	@Override
	public Dimension preferredLayoutSize(Container arg0) {
		if (arg0.getPreferredSize() == null) {
			assert (axis == X_AXIS || axis == Y_AXIS);
			int preferredWidth = (int) first.getPreferredSize().getWidth();
			int preferredHeight = (int) first.getPreferredSize().getHeight();
			if (axis == X_AXIS) {
				preferredWidth = (int) (first.getPreferredSize().getWidth() * (1 + (1 - division)
						/ division));
			} else if (axis == Y_AXIS) {
				preferredHeight = (int) (first.getPreferredSize().getHeight() * (1 + (1 - division)
						/ division));
			}
			return new Dimension(preferredWidth, preferredHeight);
		} else
			return arg0.getPreferredSize();
	}

	@Override
	public void removeLayoutComponent(Component arg0) {

	}

	/**
	 * Listener to add to parent container; This listener control division by
	 * dragging border of frames;
	 * 
	 * @author hanhe
	 * 
	 */
	public class DivisionListener extends MouseMotionAdapter {
		private Point lastPoint;
		private JPanel parent;

		public DivisionListener(JPanel parent) {
			this.parent = parent;
		}

		@Override
		public void mouseDragged(MouseEvent arg0) {
			if (lastPoint == null) {
				lastPoint = arg0.getPoint();
				return;
			}
			int dx = arg0.getX() - lastPoint.x;
			int dy = arg0.getY() - lastPoint.y;
			float shifting = 0;
			if (axis == X_AXIS) {
				shifting = (float) (dx / parent.getSize().getWidth());
			} else if (axis == Y_AXIS) {
				shifting = (float) (dy / parent.getSize().getHeight());
			}
			DividedLayout.this.division += shifting;
			parent.invalidate();
			parent.validate();
			// parent.repaint();
			lastPoint = arg0.getPoint();
		}
	}

}
